<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/25
 * Time: 16:53
 */

namespace app\admin\controller;


use app\model\Departments as DepartmentModel;
use app\model\FriendGroup;
use app\model\Users as UserModel;
use app\model\UserReal as UserRealModel;
use app\validate\JobValidate;

class User
{
    public function getAllUserByPage(){
        if(request()->isPost()){
            $UserModel = new UserModel();
            $count = $UserModel->getAllCount();
            $page = 1;
            $limit = 10;
            if(input('page')){
                $page = input('page');
            }
            if(input('limit')){
                $limit = input('limit');
            }
            $res_data = $UserModel->getAllUserByPage($page,$limit);
            return res(1,'',$res_data,$count);
        }
        return res(0,MSG_NOT_POST);
    }
    public function getAllName(){
        if(request()->isPost()){
            $UserModel = new UserModel();
            $res_data = $UserModel->getAllName();
            return res(1,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }
    /**
     * 动态修改用户数据
     * @return false|string
     */
    public function edit(){
        if(request()->isPost()) {
            //过滤接收参数
            $user_data = request()->only('user_id,user_pwd,user_name,user_phone,user_email,user_sex,user_birthday,user_header,user_info,user_address,department_id,role_id');
            //判断是否存在用户标识
            if(isset($user_data['user_id'])) {
                $id = $user_data['user_id'];
                //更新集中移除用户标识
                unset($user_data['user_id']);
                $user = new UserModel();
                $res = $user->edit($id, $user_data);
                if ($res) {
                    $user_res = $user->getUserById($id);
                    return res(1, '修改成功',$user_res);
                }
                return res(0, '修改失败',$user_data);
            }
            return res(0,'缺少用户标识');
        }
        return res(0,'请求方式错误');
    }
    public function signIn(){
        if(request()->isPost()){
            //$data = request()->only('user_name,user_birthday,political_identity,user_degree,user_sex,user_national,user_health,graduation_time,user_header,real_name,user_address,user_email,department_id,user_pwd,real_card,real_address,user_phone,role_id,confirm_pwd,user_info');
            $user_data = request()->only('user_pwd,user_name,user_phone,user_email,user_sex,user_birthday,user_header,address_id,user_info,user_address,department_id,role_id');
            $user_real_data = request()->only('real_card,real_name,user_health,graduation_time,user_national,political_identity,user_degree');
            $JobValidate = new JobValidate();
            if(!$JobValidate->check(array_merge($user_data,$user_real_data))) return res(0,$JobValidate->getError());
            $user_data['user_birthday'] = strtotime($user_data['user_birthday']);

            $user_data['registe_time'] = time();
            $user_data['user_status'] = 1;
            $user_data['user_ip'] = request()->ip();
            $user_data['user_pwd'] = md5($user_data['user_pwd']);
            $UserModel = new UserModel();
            $UserModel->startTrans();
            $user_signin = $UserModel->signIn($user_data);
            if(!$user_signin){
                $UserModel->rollback();
                return res(0,'登记失败');
            }
            $user_real_data['user_id'] = $user_signin['user_id'];
            $user_real_data['graduation_time'] = strtotime($user_real_data['graduation_time']);
            $user_real_data['real_time'] = time();
            $UserRealModel = new UserRealModel();
            $UserRealModel->startTrans();
            $real_res = $UserRealModel->add($user_real_data);
            if(!$real_res){
                $UserModel->rollback();
                $UserRealModel->rollback();
                return res(0,'登记失败');
            }
            $friendgroup = new FriendGroup();
            $res_friendgroup  = $friendgroup->addDefault($user_signin['user_id'],time());
            if($res_friendgroup){
                $UserRealModel->commit();
                $UserModel->commit();
                return res(1,'登记成功');
            }
            $UserModel->rollback();
            $UserRealModel->rollback();
            return res(0,'登记失败');
        }
        return res(0,MSG_NOT_POST);
    }

    public function getUserDetail(){
        if(request()->isPost()){
            $user_id = input('user_id');
            $UserModel = new UserModel();
            $user_detail = $UserModel->getDetail($user_id);
            return res(1,'',$user_detail);
        }
        return res(0,MSG_NOT_POST);
    }
    public function userEdit(){
        if(request()->isPost()){
            $user_data = request()->only('user_pwd,user_name,user_phone,user_email,user_sex,user_birthday,user_header,address_id,user_info,user_address,department_id,role_id');
            $user_real_data = request()->only('real_card,real_name,user_health,graduation_time,user_national,political_identity,user_degree');
            $user_id = input('user_id');
            $UserModel = new UserModel();
            $UserRealModel = new UserRealModel();
            $UserModel->startTrans();
            if(count($user_data) > 0){
                if(isset($user_data['user_birthday'])) $user_data['user_birthday'] = strtotime($user_data['user_birthday']);
                if(!$UserModel->edit($user_id,$user_data)){
                    $UserModel->rollback();
                    return res(0,'修改失败');
                };
            }
            if(count($user_real_data) > 0){
                if(isset($user_real_data['graduation_time'])) $user_real_data['graduation_time'] = strtotime($user_real_data['graduation_time']);
                if(!$UserRealModel->editByUserId($user_id,$user_real_data)){
                    $UserModel->rollback();
                    return res(0,'修改失败');
                };
            }
            $UserModel->commit();
            return res(1,'修改成功',$user_data);
        }
        return res(0,MSG_NOT_POST);
    }

}